import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.script.Calculations;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSItem;
import com.speljohan.rsbot.script.wrappers.RSItemTile;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class MDragPwn3r extends Script implements PaintListener {

	public MDragPwn3r() {
		super();
		setParameter("author", "Mouchicc - Mystic Team");
		setParameter("version", "1.0");
		setParameter("name", "MysticAZombies");
		setParameter("category", "Combat");
	}

	RSTile startPosition;
	int prayAt;
	int startLvl;
	int stat;
	int fightMode;
	int spell;
	int draggyID;
	String skill;
	String style;
	int eatAT;

	public int[] charms = { 12158, 12159, 12160, 12163 };
	public int ITPID = 0000;
	int tehArrows;
	public boolean doingspec;
	public boolean potion;
	public boolean charmpickup;
	public boolean takeSomeItem;
	public boolean ranging;
	int randomWield = random(70, 120);
	int CurSTAT;
	int RealSTAT;
	public static int[] potID;

	boolean anyCharms = false;
	boolean anyCustoms = false;
	boolean anyArrows = false;

	boolean prayer = false;
	boolean Piety = false;
	boolean Chivalry = false;
	boolean MysticMight = false;
	boolean EagleEye = false;
	boolean UltimateStrength = false;
	boolean IncredibleReflexes = false;

	RSNPC dragon;

	long thisTime = System.currentTimeMillis();
	long antiBTime = System.currentTimeMillis() + random(250000, 50000);

	long startTime = System.currentTimeMillis();
	int startExp;
	int killed = -1;
	int prayerPoints;

	public static int[] FoodID = { 1895, 1893, 1891, 4293, 2142, 291, 2140,
			3228, 9980, 7223, 6297, 6293, 6295, 6299, 7521, 9988, 7228, 2878,
			7568, 2343, 1861, 13433, 315, 325, 319, 3144, 347, 355, 333, 339,
			351, 329, 3381, 361, 379, 380, 10136, 5003, 379, 365, 373, 374,
			7946, 385, 397, 391, 3369, 3371, 3373, 2309, 2325, 2333, 2327,
			2331, 2323, 2335, 7178, 7180, 7188, 7190, 7198, 7200, 7208, 7210,
			7218, 7220, 2003, 2011, 2289, 2291, 2293, 2295, 2297, 2299, 2301,
			2303, 1891, 1893, 1895, 1897, 1898, 1899, 1900, 1901, 1902, 7072,
			7062, 7078, 7064, 7084, 7082, 7066, 7068, 1942, 6701, 6703, 7054,
			6705, 7056, 7060, 2130, 1985, 1993, 1989, 1978, 5763, 5765, 1913,
			5747, 1905, 5739, 1909, 5743, 1907, 1911, 5745, 2955, 5749, 5751,
			5753, 5755, 5757, 5759, 5761, 2084, 2034, 2048, 2036, 2217, 2213,
			2205, 2209, 2054, 2040, 2080, 2277, 2225, 2255, 2221, 2253, 2219,
			2281, 2227, 2223, 2191, 2233, 2092, 2032, 2074, 2030, 2281, 2235,
			2064, 2028, 2187, 2185, 2229, 6883, 1971, 4608, 1883, 1885 };

	// What to load on Start
	@Override
	public boolean onStart(Map<String, String> args) {
		startPosition = getMyPlayer().getLocation();
		startLvl = skills.getCurrentSkillLevel(stat);
		prayAt = Math
				.round((skills.getCurrentSkillLevel(STAT_PRAYER) / 100) * 30);

		if (args.get("dragon") == "Bronze Dragon") {
			draggyID = 1590;
		} else if (args.get("dragon") == "Iron Dragon") {
			draggyID = 1591;
		} else if (args.get("dragon") == "Steel Dragon") {
			draggyID = 1592;
		}

		String spellChosen = args.get("spells");
		String[] spells = { "Wind Bolt", "Fire Bolt", "Water Bolt",
				"Earth Bolt" };
		int[] spellsInt = { 1, 2, 3, 4 };
		int chosenSpellForUse;

		for (int c = 0; c < spellsInt.length; c++) {
			if (spellChosen.equals(spells[c])) {
				chosenSpellForUse = spellsInt[c];
			}
		}

		if (args.get("skill").equals("Attack")) {
			stat = STAT_ATTACK;
			skill = "Attack";
			fightMode = 0;
			potID = new int[] { 121, 122, 123, 124, 125, 126, 145, 146, 147,
					148, 149, 150, 2428, 2429, 2436, 2437, 11429, 11430, 11431,
					11432, 9739, 9740, 9741, 9742, 9743, 9744, 9745, 9746 };
		}
		if (args.get("skill").equals("Strength")) {
			stat = STAT_STRENGTH;
			skill = "Strength";
			fightMode = 1;
			potID = new int[] { 113, 114, 115, 116, 117, 118, 119, 120, 157,
					158, 159, 160, 161, 162, 2440, 2441, 11441, 11442, 11443,
					11444, 9739, 9740, 9741, 9742, 9743, 9744, 9745, 9746 };

		}
		if (args.get("skill").equals("Defence")) {
			stat = STAT_DEFENSE;
			skill = "Defence";
			fightMode = 3;
			potID = new int[] { 133, 134, 135, 136, 137, 138, 163, 164, 165,
					166, 167, 168, 2432, 2433, 2442, 2443, 11457, 11458, 11459,
					11460, 9739, 9740, 9741, 9742, 9743, 9744, 9745, 9746 };
		}
		if (args.get("skill").equals("Accurate Ranged")) {
			stat = STAT_RANGE;
			skill = "Ranged";
			fightMode = 0;
			potID = new int[] { 169, 170, 171, 172, 173, 174, 2444, 2445,
					11509, 11510, 11511, 11512 };
		}
		if (args.get("skill").equals("Rapid Ranged")) {
			stat = STAT_RANGE;
			skill = "Ranged";
			fightMode = 1;
			potID = new int[] { 169, 170, 171, 172, 173, 174, 2444, 2445,
					11509, 11510, 11511, 11512 };
		}
		if (args.get("skill").equals("Longrange Ranged")) {
			stat = STAT_RANGE;
			skill = "Ranged";
			fightMode = 3;
			potID = new int[] { 169, 170, 171, 172, 173, 174, 2444, 2445,
					11509, 11510, 11511, 11512 };
		}

		if (!args.get("eatt").isEmpty()) {
			eatAT = (skills.getCurrentSkillLevel(3) / 100) * random(25, 33);
		} else {
			eatAT = Integer.parseInt(args.get("eatt") + random(-2, 5));
		}

		return true;
	}

	// Returns if there are some items that has to be looten
	public boolean anyLootItems() {
		RSTile charmItems = getGroundItemByID(charms);
		RSTile Arrowz = getGroundItemByID(tehArrows);
		RSTile someCustomItem = getGroundItemByID(ITPID);

		// Picks up charms
		if (charmItems != null && charmpickup) {
			return anyCharms = true;
		}

		// Picks up any item
		if (someCustomItem != null && takeSomeItem) {
			return anyCustoms = true;
		}

		// Picks up arrows if ranging
		if (Arrowz != null && getItemCount(tehArrows) >= 5 && ranging) {
			return anyArrows = true;
		}

		return false;
	}

	private int getItemCount(int... ids) {
		RSItemTile tile = getGroundItemByID(ids);
		RSItem arrow = tile.getItem();
		int stack = arrow.getStackSize();
		return stack;
	}

	public enum actions {
		FIGHTING, LOOTING, ANTIBAN, POTTING, PRAYER, WALKING_TO_MONSTER, EATING, ELIMINATE_SCRIPT
	}

	public actions getAction() {

		if (getMyPlayer().getHPPercent() <= random(30, 40)) {
			return actions.EATING;
		}

		if (thisTime > antiBTime) {
			antiBTime = System.currentTimeMillis() + random(25000, 50000);
			return actions.ANTIBAN;
		}

		CurSTAT = skills.getCurrentSkillLevel(stat);
		RealSTAT = skills.getRealSkillLevel(stat);
		if (CurSTAT - RealSTAT <= random(1, 2) && potion == true) {
			return actions.POTTING;
		}

		prayerPoints = skills.getCurrentSkillLevel(Constants.STAT_PRAYER);
		if (prayer && prayerPoints <= prayAt) {
			return actions.PRAYER;
		}

		if (anyLootItems()) {
			return actions.LOOTING;
		}

		dragon = getNearestFreeNPCByID(draggyID);
		if (dragon != null) {
			if (pointOnScreen(dragon.getScreenLocation())) {
				if (dragon.getInteracting() == null
						&& getMyPlayer().getInteracting() == null) {
					return actions.FIGHTING;
				} else {
					return actions.WALKING_TO_MONSTER;
				}
			}
		}

		return actions.ELIMINATE_SCRIPT;
	}

	@Override
	public int loop() {

		if (isFullyLoggedIn()) {
			setAttackStyle();
			setRunning();
		}

		if (getMyPlayer().getAnimation() != -1
				|| getMyPlayer().getInteracting() != null) {
			return 450;
		}

		actions whatToDo = getAction();

		switch (whatToDo) {

		case EATING:
			for (int i = 0; i < FoodID.length; i++) {
				if (getInventoryCount(FoodID[i]) >= 1) {
					clickInventoryItem(FoodID[i], "Eat");
				}
			}
			break;

		case ANTIBAN:
			break;

		case POTTING:
			CurSTAT = skills.getCurrentSkillLevel(stat);
			RealSTAT = skills.getRealSkillLevel(stat);
			if (CurSTAT - RealSTAT <= random(1, 2) && potion == true) {
				for (int i = 0; i < potID.length; i++) {
					if (getInventoryCount(potID[i]) >= 1) {
						clickInventoryItem(potID[i], "Drink");
					}
				}
			} else {
				wait(random(20, 30));
			}
			break;

		case PRAYER:
			prayerPoints = skills.getCurrentSkillLevel(Constants.STAT_PRAYER);
			if (prayer && prayerPoints <= prayAt) {
				if (getMyPlayer().getAnimation() != -1
						&& getMyPlayer().getInteracting() == null) {
					activatePrayer();
				}
			}
			break;

		case LOOTING:
			// Takes Charms
			RSTile charmItems = getGroundItemByID(charms);
			if (anyCharms == true) {
				if (charmItems != null) {
					if (getMyPlayer().getInteracting() == null
							&& !getMyPlayer().isMoving()
							&& distanceTo(charmItems) <= 2) {
						clickTile(charmItems, "charm");
						wait(random(300, 850));
					}
				}
			}
			// Takes Arrows
			RSTile Arrowz = getGroundItemByID(tehArrows);
			if (anyArrows == true) {
				if (Arrowz != null) {
					if (getMyPlayer().getInteracting() == null
							&& !getMyPlayer().isMoving()
							&& distanceTo(Arrowz) <= 2) {
						clickTile(Arrowz, "Take " + IDtoName(tehArrows));
						wait(random(300, 850));
					}
				}
			}
			// Takes Custom Items
			final RSTile someCustomItem = getGroundItemByID(ITPID);
			if (anyCustoms) {
				if (someCustomItem != null) {
					if (getMyPlayer().getInteracting() == null
							&& !getMyPlayer().isMoving()
							&& distanceTo(someCustomItem) <= 2) {
						clickTile(someCustomItem, "Take " + IDtoName(ITPID));
						wait(random(300, 850));
					}
				}
			}

			break;

		case FIGHTING:
			dragon = getNearestFreeNPCByID(draggyID);
			if (dragon != null) {
				clickNPC(dragon, "ttack");
			}
			break;

		case WALKING_TO_MONSTER:
			if (dragon != null) {
				walkTileMM(randomizeTile(dragon.getLocation(), 2, 2));
			}
			break;

		case ELIMINATE_SCRIPT:
			stopAllScripts();

		default:
			log("DEFAULT");
		}

		return random(500, 1000);
	}

	public void activatePrayer() {
		if (getCurrentTab() != TAB_PRAYER) {
			openTab(TAB_PRAYER);
		}
		if (Piety) {
			log("Piety Activated!");
			atInterface(RSInterface.getInterface(271).getChild(57));
			wait(random(200, 500));
		}
		if (Chivalry) {
			log("Chivalry Activated!");
			atInterface(RSInterface.getInterface(271).getChild(55));
			wait(random(200, 500));
		}
		if (MysticMight) {
			log("Mystic Might Activated!");
			atInterface(RSInterface.getInterface(271).getChild(45));
			wait(random(200, 500));
		}
		if (EagleEye) {
			log("Eagle Eye Activated!");
			atInterface(RSInterface.getInterface(271).getChild(43));
			wait(random(200, 500));
		}
		if (IncredibleReflexes) {
			log("Incredible Reflexes Activated!");
			atInterface(RSInterface.getInterface(271).getChild(35));
			wait(random(200, 500));
		}
		if (UltimateStrength) {
			log("Ultimate Strength Activated!");
			atInterface(RSInterface.getInterface(271).getChild(33));
			wait(random(200, 500));
		}
	}

	// Sets the attack style you want
	public void setAttackStyle() {
		if (style.equals("Ranged") || style.equals("Melee")) {
			if (fightMode != getFightMode()) {
				if (getCurrentTab() != Constants.TAB_ATTACK) {
					openTab(Constants.TAB_ATTACK);
					wait(random(150, 250));
				}
				setFightMode(fightMode);
			}
		} else if (style.equals("Magic")) {
			autocastSpell(spell);
		}
	}

	// Method for autocasting, simple but effective
	public boolean autocastSpell(final int spell) {
		if (getCurrentTab() != Constants.TAB_MAGIC) {
			openTab(Constants.TAB_MAGIC);
			wait(random(150, 250));
		}

		return atInterface(Constants.INTERFACE_TAB_MAGIC, spell, "autocast");
	}

	// Sets the running
	public void setRunning() {
		if (!isRunning() && getEnergy() > random(50, 70)) {
			setRun(true);
		}
	}

	// Eating method
	public void eat() {
		for (int i = 0; i < FoodID.length; i++) {
			if (getInventoryCount(FoodID[i]) >= 1) {
				clickInventoryItem(FoodID[i], "Eat");
			}
		}
	}

	// Takes Charms
	public void takeCharms() {
		final RSTile charmItems = getGroundItemByID(charms);
		if (charmItems != null) {
			if (getMyPlayer().getInteracting() == null
					&& !getMyPlayer().isMoving() && distanceTo(charmItems) <= 2) {
				clickTile(charmItems, "charm");
				wait(random(300, 850));
			}
		}
		return;
	}

	// Takes arrows, gets it own ID from Inventory
	public void takeArrowz() {
		final RSTile Arrowz = getGroundItemByID(tehArrows);
		if (Arrowz != null) {
			if (getMyPlayer().getInteracting() == null
					&& !getMyPlayer().isMoving() && distanceTo(Arrowz) <= 2) {
				clickTile(Arrowz, "Take " + IDtoName(tehArrows));
				wait(random(300, 850));
			}
		}
		return;
	}

	// Takes Items
	public void takeCustomItem() {
		final RSTile someCustomItem = getGroundItemByID(ITPID);
		if (someCustomItem != null) {
			if (getMyPlayer().getInteracting() == null
					&& !getMyPlayer().isMoving()
					&& distanceTo(someCustomItem) <= 2) {
				clickTile(someCustomItem, "Take " + IDtoName(ITPID));
				wait(random(300, 850));
			}
		}
		return;
	}

	// Gets Special
	public int getSpec() {
		return getSetting(300);
	}

	// Gets if Special is Enabled
	public boolean specEnabled() {
		return (getSetting(301)) == 1;
	}

	// Does Special!
	public void doSpec() {
		if (getSpec() >= 1000 && !specEnabled() && doingspec == true) {
			openTab(Constants.TAB_ATTACK);
			clickMouse(645 + random(0, 4), 425 + random(0, 4), true);
			wait(350);
		}
		return;
	}

	// Drinks potions
	public void drinkPotion() {
		CurSTAT = skills.getCurrentSkillLevel(stat);
		RealSTAT = skills.getRealSkillLevel(stat);
		if (CurSTAT - RealSTAT <= random(1, 2) && potion == true) {
			for (int i = 0; i < potID.length; i++) {
				if (getInventoryCount(potID[i]) >= 1) {
					clickInventoryItem(potID[i], "Drink");
					return;
				}
			}
		} else {
			wait(random(20, 30));
			return;
		}
	}

	public int profit() {

		int[] dragonMedHelms = new int[] { 1149, 1150, 6967, 13495 };
		int[] dragonPlateLs = new int[] { 4087, 4088, 4180, 13488 };
		int[] dragonPlateSks = new int[] { 4585, 4586 };
		int[] visages = new int[] { 11286, 11287 };

		int profitHelms = getInventoryCount(dragonMedHelms)
				* grandExchange.loadItemInfo(1149).getMarketPrice();

		int profitPlateLegs = getInventoryCount(dragonPlateLs)
				* grandExchange.loadItemInfo(4087).getMarketPrice();

		int profitPlateSkirts = getInventoryCount(dragonPlateSks)
				* grandExchange.loadItemInfo(4585).getMarketPrice();

		int profitVisages = getInventoryCount(visages)
				* grandExchange.loadItemInfo(11286).getMarketPrice();

		int profitOffAlches = getInventoryCount(995);

		int totalProfitGained = profitHelms + profitPlateLegs
				+ profitPlateSkirts + profitVisages + profitOffAlches;

		return totalProfitGained;
	}

	public void alchItems() {
		int DragonSpear = 1249;
		int ShieldLeftHalf = 2366;
		int RuneBAxe = 137;
		int KiteShield = 1201;
		int DragonDagger = 1215;
		int Rune2HSword = 1319;
		int RunePlateBody = 1127;
		int RuneSQShield = 1185;
		int RuneMedHelm = 1147;

		int[] alchIt = new int[] { DragonSpear, ShieldLeftHalf, RuneBAxe,
				KiteShield, DragonDagger, Rune2HSword, RunePlateBody,
				RuneSQShield, RuneMedHelm };

		for (int i : alchIt) {
			RSItem item = getInventoryItemByID(i);
			if (item == null) {
				continue;
			}
			if (getCurrentTab() != TAB_MAGIC) {
				openTab(TAB_MAGIC);
			}
			// Need to change mouse positions
			moveMouse(random(563, 577), random(350, 354));
			clickMouse(true);
			wait(random(400, 600));
			clickInventoryItem(i, true);
			wait(random(1400, 1600));
			if (getCurrentTab() != TAB_INVENTORY) {
				openTab(TAB_INVENTORY);
			}
		}

	}

	private int Fight() {
		final RSNPC draggy = getNearestFreeNPCByID(draggyID);

		// Eating
		if (skills.getCurrentSkillLevel(3) <= eatAT) {
			eat();
		}

		// Does Special
		if (getSpec() >= 1000 && !specEnabled() && doingspec == true) {
			doSpec();
		}

		// Wields Arrows/Knifes if you put in
		if (getInventoryCount(tehArrows) > randomWield) {
			if (getCurrentTab() != TAB_INVENTORY) {
				openTab(TAB_INVENTORY);
				wait(random(150, 200));
			}
			clickInventoryItem(tehArrows, "Wield");
		}

		// Drinks potions
		if (potion) {
			drinkPotion();
		}

		// Picks up charms
		if (charmpickup) {
			takeCharms();
		}

		// Picks up any item
		if (takeSomeItem) {
			takeCustomItem();
		}

		// Picks up arrows if ranging
		if (ranging) {
			takeArrowz();
		}

		// Activates prayer when low
		if (skills.getCurrentSkillLevel(STAT_PRAYER) <= prayAt) {
			activatePrayer();
		}

		// Attacks dragons
		if (draggy != null) {
			if (draggy.getInteracting() != null
					&& getMyPlayer().getInteracting() == null) {
				return random(800, 1400);
			}

			if (getMyPlayer().getInteracting() == null) {
				clickNPC(draggy, "ttack");
				return random(800, 1400);
			}
			return random(200, 400);
		}

		return random(200, 400);
	}

	public String IDtoName(int id) {
		try {
			URL url = new URL("http://itemdb-rs.runescape.com/viewitem.ws?obj="
					+ id);
			final BufferedReader reader = new BufferedReader(
					new InputStreamReader(url.openStream()));

			String line;
			int i = 0;
			while ((line = reader.readLine()) != null) {
				if (line.equals("<div class=" + '"' + "subsectionHeader" + '"'
						+ ">")) {
					i++;
					continue;
				}
				if (i == 1) {
					return line;
				}
			}
		} catch (Exception e) {

		}
		return null;
	}

	public String[] IDtoName(int[] ids) {
		ArrayList<String> s = new ArrayList<String>();
		for (int r = 0; r < ids.length; r++) {
			try {
				URL url = new URL(
						"http://itemdb-rs.runescape.com/viewitem.ws?obj="
								+ ids[r]);
				final BufferedReader reader = new BufferedReader(
						new InputStreamReader(url.openStream()));

				String line;
				int i = 0;
				while ((line = reader.readLine()) != null) {
					if (line.equals("<div class=" + '"' + "subsectionHeader"
							+ '"' + ">")) {
						i++;
						continue;
					}
					if (i == 1) {
						s.add(line);
						break;
					}
				}
			} catch (Exception e) {

			}
		}
		return s.toArray(new String[s.size()]);
	}

	// Clicks a NPC by Ruski
	public boolean clickNPC(RSNPC npc, String action) {
		if (npc == null) {
			return false;
		}
		RSTile tile = npc.getLocation();
		if (!tile.isValid()) {
			return false;
		}

		try {
			Point screenLoc = npc.getScreenLocation();
			if (distanceTo(tile) > 6 || !pointOnScreen(screenLoc)) {
				turnToTile(tile);
			}
			for (int i = 0; i < 12; i++) {
				screenLoc = npc.getScreenLocation();
				if (!npc.isValid() || !pointOnScreen(screenLoc)) {
					return false;
				}
				moveMouse(screenLoc, 5, 5);
				if (getMenuItems().get(0).toLowerCase().contains(
						npc.getName().toLowerCase())) {
					break;
				}
				if (getMouseLocation().equals(screenLoc)) {
					break;
				}
			}
			List<String> menuItems = getMenuItems();
			if (menuItems.isEmpty()) {
				return false;
			}
			for (int a = 0; a < menuItems.size(); a++) {
				if (menuItems.get(a).toLowerCase().contains(
						npc.getName().toLowerCase())) {
					if (menuItems.get(0).toLowerCase().contains(
							action.toLowerCase())) {
						clickMouse(true);
						return true;
					} else {
						clickMouse(false);
						return atMenu(action);
					}
				}
			}
		} catch (Exception e) {
			System.out.print("clickNPC(RSNPC, String) error: " + e);
			return false;
		}
		return false;
	}

	// Method for clicking an inventoryItem
	public boolean clickInventoryItem(int itemID, String option) {
		if (getCurrentTab() != TAB_INVENTORY) {
			openTab(TAB_INVENTORY);
		}
		int[] items = getInventoryArray();
		java.util.List<Integer> possible = new ArrayList<Integer>();
		for (int i = 0; i < items.length; i++) {
			if (items[i] == itemID) {
				possible.add(i);
			}
		}
		if (possible.size() == 0) {
			return false;
		}
		int idx = possible.get(random(0, possible.size()));
		Point t = getInventoryItemPoint(idx);
		moveMouse(t, 5, 5);
		long waitTime = System.currentTimeMillis() + random(50, 250);
		boolean found = false;
		while (!found && System.currentTimeMillis() < waitTime) {
			wait(15);
			if ((getMenuItems()).get(0).toLowerCase().contains(
					option.toLowerCase())) {
				found = true;
			}
		}
		if (found) {
			clickMouse(true);
			wait(random(150, 250));
			return true;
		}
		clickMouse(false);
		wait(random(150, 250));
		return atMenu(option);
	}

	// clickTile method by DDM
	public boolean clickTile(RSTile tile, String option) {
		Point p = Calculations.tileToScreen(tile);
		p.x += random(-5, 6);
		p.y += random(-5, 6);
		if (p.x < 0 || p.y < 0) {
			return false;
		}

		moveMouse(p);
		// wait(random(100,150));
		getMenuItems();
		@SuppressWarnings("unused")
		List<String> itemsList = getMenuItems();

		long waitTime = System.currentTimeMillis() + random(50, 250);
		boolean foundOption = false;
		while (System.currentTimeMillis() < waitTime && !foundOption) {
			wait(15);
			foundOption = (itemsList = getMenuItems()).get(0).toLowerCase()
					.contains(option.toLowerCase());
		}
		if (foundOption) {
			clickMouse(true);
			wait(random(150, 300));
			return true;
		}
		clickMouse(false);
		wait(random(150, 300));
		return atMenu(option);
	}

	private boolean clickInventoryItem(int itemID, boolean click) { // Unknown
		// author
		if (getCurrentTab() != TAB_INVENTORY
				&& !RSInterface.getInterface(INTERFACE_BANK).isValid()
				&& !RSInterface.getInterface(INTERFACE_STORE).isValid()) {
			openTab(TAB_INVENTORY);
		}
		int[] items = getInventoryArray();
		int slot = -1;
		for (int i = 0; i < items.length; i++) {
			if (items[i] == itemID) {
				slot = i;
				break;
			}
		}
		if (slot == -1) {
			return false;
		}
		Point t = getInventoryItemPoint(slot);
		clickMouse(t, 5, 5, click);
		return true;
	}

	// Paint of Script, Credits to Noobielul
	public void onRepaint(Graphics g) {
		if (isFullyLoggedIn()) {
			if (startTime == 0) {
				startTime = System.currentTimeMillis();
			}

			if (startLvl <= 0 || startExp <= 0) {
				startLvl = skills.getCurrentSkillLevel(stat);
				startExp = skills.getCurrentSkillExp(stat);
			}

			long untilhour = 0, untilmin = 0, untilsec = 0;
			int xpToNextLevel = skills.getXPToNextLevel(stat);
			int xpPerHour = (int) ((skills.getCurrentSkillExp(stat) - startExp) * (3600000D / (((double) System
					.currentTimeMillis() - (double) startTime))));
			double timeTillLvlUp = (xpToNextLevel / xpPerHour) * 60;

			untilsec = (int) (timeTillLvlUp * 3600);
			untilmin = untilsec / 60;
			untilsec -= untilmin * 60;
			untilhour = untilmin / 60;
			untilmin -= untilhour * 60;

			long millis = System.currentTimeMillis() - startTime;
			long hours = millis / (1000 * 60 * 60);
			millis -= hours * (1000 * 60 * 60);
			long minutes = millis / (1000 * 60);
			millis -= minutes * (1000 * 60);
			long seconds = millis / 1000;

			int x = 0;
			int y = 20;
			g.setColor(new Color(0, 0, 0, 175));
			g.fillRoundRect(0, 20, 220, 120, 10, 10);
			g.fillRect(4, 319, 502, 17);
			g.setFont(new Font("Papyrus", Font.BOLD, 11));
			g.setColor(Color.YELLOW);
			g
					.drawString("MysticMDragPwn3r - V" + getVersion(), x + 10,
							y += 15);
			g.setColor(Color.ORANGE);
			g.drawString("Bussy for " + hours + ":" + minutes + ":" + seconds,
					x + 10, y += 15);
			g
					.drawString(
							"Exp/hour : "
									+ (int) ((skills.getCurrentSkillExp(stat) - startExp) * (3600000D / (((double) System
											.currentTimeMillis() - (double) startTime)))),
							x + 10, y += 15);
			g.drawString("Time to lvl up : " + untilhour + ":" + untilmin + ":"
					+ untilsec, x + 10, y += 15);
			g.drawString("Profit gained : " + profit(), x + 10, y += 15);
			g.drawString("Dragons killed : " + killed, x + 10, y += 15);
			// g.drawImage(MCSeries, 55, 360, null);
			g.setColor(Color.ORANGE);
			g.fillRect(5, 320, 5 * skills.getPercentToNextLevel(stat), 15);
			g.setColor(Color.WHITE);
			g.drawString(skills.getPercentToNextLevel(stat) + "%", 250, 331);
		}
	}

	public void onFinish() {
		Bot.getEventManager().removeListener(PaintListener.class, this);
	}

}
